package top.wangjingxin.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.wangjingxin.base.Result;
import top.wangjingxin.dao.LinkDao;
import top.wangjingxin.model.link.LinkDTO;
import top.wangjingxin.util.SessionUtil;

import static top.wangjingxin.cache.ResultCache.getCache;
import static top.wangjingxin.cache.ResultCache.getDataOk;

@Service
public class LinkService {
    @Autowired
    LinkDao linkDao;
    public Result get(int type) {
        return getDataOk(linkDao.get(type));
    }

    public Result delete(int id) {
        return getCache(linkDao.delete(id));
    }

    public Result add(LinkDTO linkDTO) {
        linkDao.add(linkDTO);
        return getDataOk(linkDTO.getId());
    }

    public Result update(LinkDTO linkDTO) {
        return getCache(linkDao.update(linkDTO));
    }

    public Result login(String id, String password) {
        int count = linkDao.login(id,password);
        if(count==1){
            SessionUtil.setAttribute("id",id);
        }
        return getDataOk(count);
    }
}
